package eclipse.lib;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javax.imageio.*;
import eclipse.*;
import eclipse.util.*;
/**
 * An object representing a Form with a specified background.
 * The form's dimensions are determined by the background by default.
 */
public class StandardForm extends JFrame
{
	public static final long serialVersionUID = 1L;
	private background bImg;
	/**
	 * Redraws the form.
	 *
	 */
	public void redrawForm()
	{
		repaint();
	}
	/**
	 * Redraws the background to the form.
	 *
	 */
	public void redrawBackground()
	{
		bImg.repaint();
	}	
	/**
	 * Initializes and creates a form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 * @param uImg The path to the background as a URL.
	 */
	public StandardForm(String sTitle, int iXLoc, int iYLoc, URL uImg)
	{
		bImg = new background(uImg);
		Dimension dim = bImg.getSize();
		makeForm(sTitle, dim.width + 10, dim.height + 10, iXLoc, iYLoc);
		add(bImg);
	}
	/**
	 * Initializes and creates a form with specific properties
	 * @param sTitle The title of the form as a String.
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 * @param sImg The path to the background image as a String
	 */
	public StandardForm(String sTitle, int iXLoc, int iYLoc, String sImg)
	{
		background bImg = new background(sImg);
		Dimension dim = bImg.getSize();
		makeForm(sTitle, dim.width + 10, dim.height + 10, iXLoc, iYLoc);
		add(bImg);
	}	
	/**
	 * Initializes and creates a form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 * @param eImg The background image as an ERL path.
	 */
	public StandardForm(String sTitle, int iXLoc, int iYLoc, ERL eImg)
	{
		background bImg = new background(eImg);
		Dimension dim = bImg.getSize();
		makeForm(sTitle, dim.width + 10, dim.height + 10, iXLoc, iYLoc);
		add(bImg);		
	}
	/**
	 * Initializes and creates a form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iWidth The width of the form as an integer
	 * @param iHeight The height of the form as an integer
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 */
	public StandardForm(String sTitle, int iWidth, int iHeight, int iXLoc, int iYLoc)
	{
		makeForm(sTitle, iWidth, iHeight, iXLoc, iYLoc);
	}
	/**
	 * Generates the form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iWidth The width of the form as an integer
	 * @param iHeight The height of the form as an integer
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 */
	public void makeForm(String sTitle, int iWidth, int iHeight, int iXLoc, int iYLoc)
	{		
		setSize(iWidth, iHeight);
		setLocation(iXLoc, iYLoc);
		addWindowListener
		(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent ev)
				{					
					removeAll();
					System.gc();
					System.exit(0);
				}
			}			
		);
	}			
	/**
	 * A subclass that will have a background set to a specific image.
	 * This is used to set the background of the StandardForm Object	 	 
	 */
	private class background extends JPanel
	{
		private static final long serialVersionUID = 1L;
		private Image img;
		/**
		 * Initializes the background class with a specific Image.
		 * @param iImg
		 */
		public background(Image iImg)
		{
			setImage(iImg);
		}
		/**
		 * Initializes the background class with the path of an Image.
		 * @param sImg The path for an Image file represented with a String.
		 */
		public background(String sImg)
		{
			setImage(sImg);
		}
		/**
		 * Initializes the background class with the URL of an Image.
		 * @param uImg The path for an Image file represented with a URL.
		 */
		public background(URL uImg)
		{
			setImage(uImg);
		}
		/**
		 * Initializes the background class with the ERL of an Image.
		 * @param eImg The path for an Image file represented with an ERL.
		 */
		public background(ERL eImg)
		{
			setImage(eImg.openStream());
		}
		public void paintComponent(Graphics g) 
		{
		    g.drawImage(img, 0, 0, null);		 
		}
		/**
		 * Sets the background image to the Image specified.
		 * @param img An Image file that the JPanel's background will be set to.
		 */
		public void setImage(Image img)
		{
			this.img = img;
		    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
		    setPreferredSize(size);
		    setMinimumSize(size);
		    setMaximumSize(size);
		    setSize(size);
		    setLayout(null);		    		    	
		}
		/**
		 * Sets the background image to the Image specified.
		 * @param sImage A path to the Image file represented with a String.
		 */
		public void setImage(String sImage)
		{
			setImage(new ImageIcon(sImage).getImage());
		}
		/**
		 * Sets the background image to the Image specified.		 
		 * @param iImage An InputStream representing the Image file.
		 */
		public void setImage(InputStream iImage)
		{
			try
			{
				setImage(ImageIO.read(iImage));
			}
			catch(Exception e)
			{
				ErrorManager.handleError(e);
			}
		}
		/**
		 * Sets the background image to the Image specified.
		 * @param uImage A path to the Image file represented as a URL.
		 */
		public void setImage(URL uImage)
		{
			setImage(new ImageIcon(uImage).getImage());
		}
	}
}
